
    <div class="head"><h5 class="iList">Template Definition</h5></div>
     <form action="<?php echo url_for('templates/'.($form->getObject()->isNew() ? 'create' : 'update').(!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : '')) ?>" method="post" id="valid" class="mainForm" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
     	<div class="rowElem noborder">
     	<label for="name">Name:</label>
     		<div class="formRight"><input type="text" name="template[name]" value="<?php echo $form->getObject()->getName()?>"  class="validate[required]"  id="template_name"/></div>
     		<div class="fix"></div>
     	</div>
     	
     	<div class="rowElem noborder">
	     	<label for="directory_type">Directory:</label>
     		<div class="formRight" >
                    			<select name="directory_id" id="directory_id" onchange="loadDirectoryData()" style="width:240px;"  >
                    			
                    				<?php 
                    					
                    					foreach ($master_directories as $directory) {
                    						
                    					 $selected = $directory->getId() == $selected_directory ? "selected" : "";
                    					?>
                    						<option <?php echo $selected?> value="<?php echo $directory->getId(); ?>"><?php echo $directory->getName(); ?></option>
                    					<?php 
                    					}
                    				?>
                            	</select>     		
     		
     		</div>
     		<div class="fix"></div>
     	</div>
     
    <div style="clear:both">
		<div class="our_subtitle" style="float:left">
			Involved levels
		</div>
		<div class="our_dashed" style="float: left; height: 1px; margin-top: 10px; width: 690px;">
		
		</div>
	</div>
	<div style="clear: both; width: 715px;">
			<?php 
			$i = 0;
			foreach ($categories as $category) {
					$i++;
		       		?>
		       		<div class="widget" style="float: left; margin-left: 20px; clear: none; margin-top: 20px">
		       			<div class="head" style="height:25px"><h5 style="padding-top:2px; padding-left: 7px; font-size: 14px;"><?php echo $category->getName();?></h5></div>
		       			<?php 
		       			$root_level = Doctrine_Core::getTable('Level')->getRootLevel($category->getId());
		       			$the_table = Doctrine_Core::getTable('Level');
		       			$the_table->setChildren();
		       			$the_table->getCategoryChildrenList($root_level);
		       			foreach ($the_table->getChildren() as $level) {
		       				?>
		       				<div style="padding: 5px;">
		       					<?php $checked = $level->isMemberOfSelection($levelIds) ? "checked = 'checked'" : "" ?>
		       					<input <?php echo $checked ?> type="checkbox" id="chbx_level_<?php echo $level->getId();?>" name="chbx_level_<?php echo $level->getId();?>"  /><span style="margin-left: 5px"> <?php echo $category->getName().".".$level->getName();?></span>
		       				</div>
						<?php 
		       			}?>
		       		</div>
		       		
     				<?php 
       				if ($i % 3 == 0 ){
       					?>
       					<div style="clear: both;"></div>
       					<?php 
       				}?>
		       		<?php 
		       }?>         		


     </div>
     	
     	<div class="fix"></div>
     	

     	
        <input type="button" value="Save" class="greyishBtn submitForm" id="save" />
        
     	<?php if (!$form->getObject()->isNew()){
     		?>
     		<input type="button" value="Create New" class="greyishBtn submitForm" id="create_new" />
     	<?php 
     	} ?>
     	        
        <div class="fix"></div>
     </form>
     
     
     
<script type="text/javascript">

$("#save").click(function (event){
    submitForm();
})

$("form#valid").submit(function (event){
    event.preventDefault();
    submitForm();
})

$("#create_new").click(function (event){

    $.ajax({type: 'POST', url:' <?php echo url_for('templates/index')?>', script:true, cache:false,
            success: function(data) {
                 $('#template_super_container_div').html(data);
                 $('#template_name').attr('value',"");
             } });
             
})

    	$("#demo3")
    		.jstree({ "plugins" : ["themes","html_data","ui", "checkbox"], 
        		"checkbox": {
    	        "real_checkboxes": true,
    	        "two_state": true
    	    } })

function submitForm(){
    var str=$('form#valid').serialize();

    $.ajax({type: 'POST', url:' <?php echo url_for('templates/'.($form->getObject()->isNew() ? 'create' :'update').
    (!$form->getObject()->isNew() ? '?id='.$form->getObject()->getId() : ''))?>', script:true, cache:false, data:  str, 
    beforeSend: function(){
        if ($("#template_name").attr("value") == "" || $('input:checked', $('form#valid')).length == 0)  return false;},
            success: function(data) {
                 $('#template_super_container_div').html(data);
                 $('#template_name').attr('value',"");
             } });
}

		
		$('form').jqTransform({imgPath:'../images/forms'});

		function loadDirectoryData (){

			var php_url = '<?php echo url_for('templates/loadDirectoryData?directory_id=')?>';
			var directory_id = document.getElementById('directory_id').value;
			php_url = php_url.concat(directory_id);	
			
			$.ajax({type: 'POST', url:php_url, script:true, cache:false, 
				success: function(data) { 
					 $('#template_super_container_div').html(data);
					 $('#templates_name').attr('value',"");
					} });
			
		}
</script>     
